<?php
/**
 * ooPHP - object oriented PHP framework
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 *
 */

/**
 * Blog note Data Transfer Object
 * @author Kamil Mikolajczyk ( rego (at) gazeta (dot) pl )
 * @version 1.0
 */
 include_once('db.class.php');
 include_once('printer.class.php');
 
class BlogNoteDto{

	private $id = "";
	var $date = "";
	var $title = "";
	var $content = "";
	var $privacyLevel = "";
	var $author = "";
	
	/**
	 * Constructor
	 * Fills all fields
	 */
	public function BlogNoteDto($i, $d, $t, $c, $pL, $a)
	{
		$this -> id = $i;
		$this -> date = $d;
		$this -> title = $t;
		$this -> content = $c;
		$this -> privacyLevel = $pL;
		$this -> author = $a;
	}
	
	public function getId()
	{
		return $this -> id;
	}
	
	public function getDate()
	{
		return $this -> date;
	}
	
	public function getAuthor()
	{
		return $this -> author;
	}
	
	public function getTitle()
	{
		return $this -> title;
	}
	
	public function getContent()
	{
		return $this -> content;
	}
	
	public function getPrivacyLevel()
	{
		return $this -> privacyLevel;
	}

}

class BlogNotesManager{

	/*
	TODO: month, year handling 
	*/

	private $db = null;
	
	function BlogNotesManager($db)
	{
		$this -> db = $db;
	}

	function createTable()
	{
	}
	
	function addBlogNote($blogNoteDto)
	{
	}
	
	/**
	 * Updates a blog note in db with data given in dto
	 * @returns int
	 */
	function updateBlogNote($dto)
	{
		$d = $dto -> getDate();
		$t = $dto -> getTitle();
		$c = $dto -> getContent();
		$pL = $dto -> getPrivacyLevel();
		$a = $dto -> getAuthor();
		$id = $dto -> getId();
		$this -> db -> executeQuery("update notes set date = $d, title = $t, content = $c, privacyLevel = $pL, author = $a where id = $id");
		$row = $this -> db -> fetch();
		return BlogNotesManager::row2dto($row[0]);
	}
	
	/**
	 * Converts a row taken from db to dto object
	 * @var array
	 * @returns BlogNoteDto
	 */
	static function row2dto($row)
	{
		$i = $row['idn'];
		$d = $row['date'];
		$t = $row['title'];
		$c = $row['content'];
		$pL = $row['privacyLevel'];
		$a = $row['idu'];
		return new BlogNoteDto($i, $d, $t, $c, $pL, $a);
	}
	
	/**
	 * Gets a single blog note dto with given id
	 * @var int
	 */
	function getBlogNote($id)
	{
		$this -> db -> executeQuery("select * from notes where idn = $id");
		$row = $this -> db -> fetch();
		return BlogNotesManager::row2dto($row[0]);
	}
	
	/**
	 * Gets a range of blog note dtos for given year
	 * @var int
	 */
	function getBlogNotesByYear($year)
	{
		$this -> db -> executeQuery("select * from notes where year = ".$year);
		$row = $this -> db -> fetchAll();
		$dtos = Array();
		foreach($row as $r)
			$dtos[] = BlogNotesManager::row2dto($r);
		return $dtos;
	}
	
	/**
	 * Gets a range of blog note dtos for given year and month
	 * @var int
	 */
	function getBlogNotesByMonth($year, $month)
	{
		$this -> db -> executeQuery("select * from notes where year = ".$year." and month = ".$month);
		$row = $this -> db -> fetchAll();
		$dtos = Array();
		foreach($row as $r)
			$dtos[] = BlogNotesManager::row2dto($r);
		return $dtos;
	}
	
	function getBlogNotesRange($id, $howMany)
	{
	}
}

class BlogNoteView{

	var $layout = null;
	
	var $id = "";
	var $title = "";
	var $date = "";
	var $author = "";
	var $content = "";
	var $comments = 0;
	static $commentsLabel = "Komentarze: ";
	static $commentsLink = "?note=";

	public function BlogNoteView($dto)
	{
		$this -> id = $dto -> getId();
		$this -> title = $dto -> getTitle();
		$this -> date = $dto -> getDate();
		// TODO: Author's name
		$this -> author = $dto -> getAuthor();
		$this -> content = $dto -> getContent();
		// TODO: Comments count
		$this -> comments = 5;
	}
	
	public function setLayout($l)
	{
		$this -> layout = $l;
	}
	
	public function render($indents = 0)
	{
		if($layout != null)
			$layout -> render($indents);
		else
		{
			Printer::writeln('<div class="note">', $indents);
			Printer::writeln('<h3 class="noteTitle">'.$this -> title.'</h3>', $indents + 1);
			Printer::writeln('<p class="noteAuthor">'.$this -> author.'</p>', $indents + 1);
			Printer::writeln('<p class="noteDate">'.$this -> date.'</p>', $indents + 1);
			Printer::writeln('<p class="noteContent">'.$this -> content.'</p>', $indents + 1);
			// TODO: Comments link
			if($this -> comments > 0)
				Printer::writeln('<a class="noteCommentsLink" href="'.BlogNoteView::$commentsLink.$this->id.'">'.BlogNoteView::$commentsLabel.$this->comments.'</a>', $indents + 1);
			Printer::writeln('</div>', $indents);
		}
		
	}
}

$db = new DBManager('localhost', 'root', '', 'blog');
$bnm = new BlogNotesManager($db);
$dtos = $bnm -> getBlogNotesByYear(2010);
foreach($dtos as $dto)
{
	$bnv = new BlogNoteView($dto);
	$bnv -> render(1);
}
echo '<hr>';
$bnv = new BlogNoteView($bnm -> getBlogNote(2));
$bnv -> render(1);
?>